package com.cn.lyl.dao.impl;

import com.cn.lyl.dao.NavDao;
import com.cn.lyl.model.NavModel;
import com.cn.lyl.util.JDBC;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NavDaoImpl implements NavDao {
    @Override
    public int add(NavModel navModel) {

        String sql = "insert into nav (title,href,create_time,update_time,enable,remark) values" +
                "('"+navModel.getTitle()+"','"+navModel.getHref()+"',now(),now(),1,'')";

        return JDBC.update(sql);
    }

    @Override
    public int update(NavModel navModel) {

        String sql = "update nav set ";
        StringBuffer stringBuffer = new StringBuffer();
        if(!"".equals(navModel.getTitle())&&navModel.getTitle()!=null) {
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" title = '"+navModel.getTitle()+"'");
        }
        if(!"".equals(navModel.getHref())&&navModel.getHref()!=null) {
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" href = '"+navModel.getHref()+"'");
        }
        if(stringBuffer.length()>0){
            stringBuffer.append(", update_time = now()");
        }
        if(stringBuffer.length()>0){
            stringBuffer.append(",");
        }

        stringBuffer.append(" remark='' where id="+navModel.getId());

        sql=sql+stringBuffer.toString();
        System.out.println("修改"+sql);
        return JDBC.update(sql);
    }

    @Override
    public int delete(NavModel navModel) {

        String sql="delete from news where id ="+navModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public NavModel findById(int id) {
        String sql ="select id , title ,href from nav  where id= "+id;
        return setList(sql).size()>0?setList(sql).get(0):null;
    }

    @Override
    public List<NavModel> findAll(NavModel navModel) {
        String sql ="select id , title ,href from nav where 1=1";
        String stringSql = commonSql(navModel);
        int pageCurr=navModel.getPage().getPageCurr();
        int pageSize=navModel.getPage().getPageSize();
        stringSql +=" order by id desc limit "+(pageCurr-1)*pageSize+","+pageSize;
        sql = sql + stringSql;
        return setList(sql);
    }

    @Override
    public int countFindAll(NavModel navModel) {
        String sql ="select count(id) count from nav where 1=1";
        String stringSql = commonSql(navModel);
        sql = sql + stringSql;
        ResultSet query = JDBC.query(sql);
        int size = 0;
        try {
            while (query.next()) {
                size = query.getInt("count");
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return size;
    }

    private List<NavModel> setList(String sql) {

        ResultSet resultSet = JDBC.query(sql);
        List<String> colList = new ArrayList<>();
        try {
            ResultSetMetaData metaData = resultSet.getMetaData();
            for (int i = 1; i <=metaData.getColumnCount(); i++) {

                colList.add(metaData.getColumnLabel(i));

            }
        } catch (SQLException e) {
            return null;
        }
        List<NavModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                NavModel model = new NavModel();
                if (colList.contains("id")) {
                    model.setId(resultSet.getInt("id"));
                }
                if (colList.contains("title")) {
                    model.setTitle(resultSet.getString("title"));
                }
                if (colList.contains("href")) {
                    model.setHref(resultSet.getString("href"));
                }
                if (colList.contains("create_time")) {
                    model.setCreateTime(resultSet.getString("create_time"));
                }
                if (colList.contains("update_time")) {
                    model.setUpdateTime(resultSet.getString("update_time"));
                }
                if (colList.contains("enable")) {
                    model.setEnable(resultSet.getInt("enable"));
                }

                if (colList.contains("remark")) {
                    model.setRemark(resultSet.getString("remark"));
                }

                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    private String commonSql(NavModel navModel) {
        StringBuffer stringBuffer = new StringBuffer();
        if (navModel.getTitle() != null && !"".equals(navModel.getTitle())) {
            stringBuffer.append(" and title like'%" +navModel.getTitle() + "%'");
        }

        return stringBuffer.toString();
    }

}
